package javase.io;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.io.Serializable;
import java.util.ArrayList;

public class StreamDemo {

	public static void main(String[] args) {
		PrintWriter pw = new PrintWriter(System.out, true);
		/*
		 * String a=new String("a"); String b=new String("a"); String c=a;
		 * pw.println("a==b?:"+(a==b)); pw.println("a.equals(b)?:"+a.equals(b));
		 * pw.println("a==c?:"+(a==c)); pw.println("a.equals(c)?:"+a.equals(c));
		 * 
		 * pw.println("Integer.max = "+Integer.MAX_VALUE);
		 * pw.println("Integer.min = "+Integer.MIN_VALUE);
		 * pw.println(Math.round(11.5)+" "+Math.round(-11.5));
		 */
		pw.println("Default fileEnconding:"	+ System.getProperty("file.encoding"));
		try {
			FileOutputStream fos = new FileOutputStream("fos.txt");
			fos.write("�ֽ�������".getBytes("GBK"));
			fos.close();
			FileInputStream fis = new FileInputStream("fos.txt");
			byte[] b = new byte[100];
			int lenzj = fis.read(b);
			fis.close();
			pw.println(new String(b, 0, lenzj));

			PrintWriter pwzf = new PrintWriter("fos.txt");
			pwzf.write("�ַ������� FIRST");
			pwzf.close();
			FileReader frzf1 = new FileReader("fos.txt");
			char[] c1 = new char[100];
			int lenzf1 = frzf1.read(c1);
			frzf1.close();
			pw.println(new String(c1, 0, lenzf1));

			FileWriter fwzf = new FileWriter("fos.txt");
			fwzf.write("�ַ������� SECOND");
			fwzf.close();
			FileReader frzf2 = new FileReader("fos.txt");
			char[] c2 = new char[100];
			int lenzf2 = frzf2.read(c2);
			frzf2.close();
			pw.println(new String(c2, 0, lenzf2));

			BufferedReader br = new BufferedReader(new InputStreamReader(
					new FileInputStream("fos.txt")));
			String str = br.readLine();
			pw.println("BuffedReader ���ԣ�" + str);

			// �����д��Ͷ�ȡ
			ObjectOutputStream oos = new ObjectOutputStream(
					new FileOutputStream("fos.txt"));
			Student s1 = new Student("writeObject1", 1);
			Student s2 = new Student("writeObject2", 2);
			oos.writeObject(s1);
			oos.writeObject(s2);
			oos.close();
			ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
					"fos.txt"));
			ArrayList<Object> alstu = new ArrayList<Object>();
			try {
				alstu.add(ois.readObject());
				alstu.add(ois.readObject());
				System.out.println(ois.readObject());
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
			pw.println(alstu);

		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

class Student implements Serializable {
	private static final long serialVersionUID = -7901494118011032650L;
	private String name;
	private int age;

	public Student() {
	}

	public Student(String name, int age) {
		this.name = name;
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String toString() {
		return "name=" + name + " age= " + age;
	}
}